package com.anti.core.config.druid;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import com.anti.commom.context.constant.ConstantContextHolder;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description TODO
 * @Author anTi
 * @Date 2020-9-30
 */
@Configuration
public class DruidConfiguration {
    //注册后台监控页面，
    @Bean
    public ServletRegistrationBean statViewServlet(){
        //StatViewServlet配置后台监控
        ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
        Map<String,String> map = new HashMap();
        //具体可配置的参数见源码
        //用户名 密码实际项目种在数据库中读取
        map.put("loginUsername",ConstantContextHolder.getDruidMonitorUsername());//登录用户名 非改用户命则自动跳转到登录页面
        map.put("loginPassword", ConstantContextHolder.getDruidMonitorPassword());//登录密码  同上
        //map.put("allow","localhost");//只允许localhost访问
        map.put("allow","");//允许所有访问
        //map.put("deny","192.168.1.1");//不允许指定ip访问
        bean.setInitParameters(map);
        return bean;
    }
    /**
     * 注册一个：filterRegistrationBean
     * @return
     *
     */
    @Bean
    public FilterRegistrationBean druidStatFilter2() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
        // 添加过滤规则.
        filterRegistrationBean.addUrlPatterns("/*");
        // 添加不需要忽略的格式信息.
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return filterRegistrationBean;
    }
}
